<template>
  <el-card class="spulist">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.categoty3Id"
      @click="emit('setIsShow', 2)"
      >添加Spu</el-button
    >
    <el-table :data="spuList" border class="spuList-table">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="Spu名称" prop="spuName" />
      <el-table-column label="Spu描述" prop="description" />
      <el-table-column label="操作" prop="name" width="280">
        <template v-slot="{ row }">
          <el-tooltip content="添加Sku" placement="top" effect="light">
            <el-button
              size="small"
              type="primary"
              :icon="Plus"
              @click="addSpuInfo(row)"
            />
          </el-tooltip>

          <el-tooltip content="修改Spu" placement="top" effect="light">
            <el-button
              size="small"
              type="warning"
              :icon="Edit"
              @click="showUpdateSpu(row)"
            />
          </el-tooltip>

          <el-tooltip content="查看Sku列表" placement="top" effect="light">
            <el-button size="small" type="info" :icon="InfoFilled" />
          </el-tooltip>

          <el-tooltip content="删除Spu" placement="top" effect="light">
            <el-button
              size="small"
              type="danger"
              :icon="Delete"
              @click="delSpuList(row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SPuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import { getSpuListApi, deleteSpuApi } from "@/api/product/spu";
import type { SpuList, TSpuItem } from "@/api/product/modles/spuModels";
import { userCategoryStore } from "@/stores/category";
import { ElMessage } from "element-plus";

const spuList = ref<SpuList>([]);
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
// 父传过来的参数
const emit = defineEmits(["setIsShow", "setSpu"]);

const categoryStore = userCategoryStore();
// 发送请求的方式
const getSpuList = async () => {
  const result = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.categoty3Id as number,
  });
  // console.log('===========',result.records);

  spuList.value = result.records;
  total.value = result.total;
};
// 当页码条数发生时触发的函数
const handleSizeChange = (newPageSize: number) => {
  pageSize.value = newPageSize;
  getSpuList();
};
// 当前页发生变化时触发的函数
const handleCurrentChange = (newPage: number) => {
  currentPage.value = newPage;
  getSpuList();
};

// 监视3级id id发生改变了就发送请求
watch(
  () => categoryStore.categoty3Id,
  (newVal) => {
    if (newVal) {
      getSpuList();
      return;
    }
    spuList.value = [];
    currentPage.value = 1;
    pageSize.value = 3;
    total.value = 0;
  },
  {
    immediate: true,
  }
);
// 点击添加
const addSpuInfo = (row: TSpuItem) => {
  emit("setIsShow", 3);
  emit("setSpu",row);
};

// 点击修改，更改组件，并把数据传个父组件
const showUpdateSpu = (row: TSpuItem) => {
  emit("setIsShow", 2);
  emit("setSpu", {
    ...row,
    spuImageList: [],
    spuSaleAttrList: [],
  });
};

// 点击删除按键
const delSpuList = async (row: TSpuItem) => {
  await deleteSpuApi(row.id);
  ElMessage.success("删除成功");
  getSpuList();
};
</script>

<style scoped>
.spulist {
  margin: 20px 0;
}
.spuList-table {
  margin: 20px 0;
}
</style>